package com.itheima.ex;

import com.alibaba.fastjson.JSON;
import com.itheima.entity.Result;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author yunfei
 * @create 2020-03-02
 */
public class MyEx implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest,
                       HttpServletResponse httpServletResponse,
                       AccessDeniedException e) throws IOException, ServletException {
        // 如果是异步请求 我们响应回去的数据都直接返回到异步请求的回调函数中
        Result result = new Result(false, "删除检查项权限不足");
        // 手动把Result转成JSON
        String ss = JSON.toJSON(result).toString();
        // 处理响应的中文乱码
        httpServletResponse.setContentType("application/json;charset=utf-8");
        httpServletResponse.getWriter().print(ss);
    }
}
